package cvr100demo;

import com.dangdi.controller.websokectHandler;
import com.gexin.fastjson.JSON;
import org.apache.http.HttpResponse;
import org.xvolks.jnative.JNative;
import org.xvolks.jnative.Type;
import org.xvolks.jnative.exceptions.NativeException;
import org.xvolks.jnative.pointers.Pointer;
import org.xvolks.jnative.pointers.memory.MemoryBlockFactory;
import util.HttpClientUtil;
import util.PropertiesUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

public class IdCardReader implements Runnable {
    static String strTmp = "";
    int len = 0;
    int ret = 0;
    private int InitCode = -1;
    private int AuthenticateCode = -1;
    private int Read_ContentCode = -1;
    private boolean flag = true;
    private String ip = "";
    private Map<String, Object> people = new HashMap<String, Object>();
    String peopleNum = "";

    public int getRead_ContentCode() {
        return Read_ContentCode;
    }

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    public String getIp() {
        return ip;
    }

    final String filePath = System.getProperty("user.dir") + "\\conf.properties";
    private PropertiesUtils utils = new PropertiesUtils(filePath);

    public void setIp(String ip) {
        this.ip = ip;
    }

    public void setRead_ContentCode(int read_ContentCode) {
        Read_ContentCode = read_ContentCode;
    }

    public int getAuthenticateCode() {
        return AuthenticateCode;
    }

    public void setAuthenticateCode(int authenticateCode) {
        AuthenticateCode = authenticateCode;
    }

    public int getInitCode() {
        return InitCode;
    }

    public void setInitCode(int initCode) {
        InitCode = initCode;
    }

    public int CVR_InitComm(int Port) throws NativeException, IllegalAccessException, UnsupportedEncodingException {
        JNative n = null;
        try {
            n = new JNative(System.getProperty("user.dir") + "\\libs\\CVR100\\Termb.dll", "CVR_InitComm");
            n.setRetVal(Type.INT); // 指定返回参数的类型
            n.setParameter(0, Port);
            n.invoke(); // 调用方法
            return Integer.parseInt(n.getRetVal());
        } finally {

        }
    }

    public int CVR_Authenticate() throws NativeException, IllegalAccessException {
        JNative n = null;
        try {
            n = new JNative(System.getProperty("user.dir") + "\\libs\\CVR100\\Termb.dll", "CVR_Authenticate");
            n.setRetVal(Type.INT); // 指定返回参数的类型
            n.invoke(); // 调用方法
            return Integer.parseInt(n.getRetVal());
        } finally {

        }
    }

    public int CVR_Read_Content(int Active) throws NativeException, IllegalAccessException {
        JNative n = null;
        try {
            n = new JNative(System.getProperty("user.dir") + "\\libs\\CVR100\\Termb.dll", "CVR_Read_Content");
            n.setRetVal(Type.INT); // 指定返回参数的类型
            n.setParameter(0, Active);
            n.invoke(); // 调用方法
            return Integer.parseInt(n.getRetVal());
        } finally {

        }
    }

    public int GetPeopleName() throws NativeException, IllegalAccessException {
        JNative n = null;
        try {
            n = new JNative(System.getProperty("user.dir") + "\\libs\\CVR100\\Termb.dll", "GetPeopleName");
            n.setRetVal(Type.INT); // 指定返回参数的类型
            Pointer a = new Pointer(MemoryBlockFactory.createMemoryBlock(4 * 10));
            Pointer b = new Pointer(MemoryBlockFactory.createMemoryBlock(4 * 30));
            n.setParameter(0, b);
            n.setParameter(1, a);
            n.invoke();
            byte[] by = new byte[120];
            by = b.getMemory();
            try {
                strTmp = new String(by, "gb2312");
                people.put("PeopleName", strTmp.trim());
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(CVR100DemoView.class.getName()).log(Level.SEVERE, null, ex);
            }

            byte[] bt = new byte[40];
            bt = a.getMemory();
            len = a.getAsInt(0);
            a.dispose();
            b.dispose();
            return Integer.parseInt(n.getRetVal());
        } finally {

        }
    }

    public int GetPeopleSex() throws NativeException, IllegalAccessException {
        JNative n = null;
        try {
            n = new JNative(System.getProperty("user.dir") + "\\libs\\CVR100\\Termb.dll", "GetPeopleSex");
            n.setRetVal(Type.INT); // 指定返回参数的类型
            Pointer a = new Pointer(MemoryBlockFactory.createMemoryBlock(4 * 10));
            Pointer b = new Pointer(MemoryBlockFactory.createMemoryBlock(4 * 30));
            n.setParameter(0, b);
            n.setParameter(1, a);
            n.invoke();
            byte[] by = new byte[120];
            by = b.getMemory();
            try {
                strTmp = new String(by, "gb2312");
                people.put("PeopleSex", strTmp.trim());
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(CVR100DemoView.class.getName()).log(Level.SEVERE, null, ex);
            }

            byte[] bt = new byte[40];
            bt = a.getMemory();
            len = a.getAsInt(0);
            a.dispose();
            b.dispose();
            return Integer.parseInt(n.getRetVal());
        } finally {

        }
    }


    public int GetPeopleNation() throws NativeException, IllegalAccessException {
        JNative n = null;
        try {
            n = new JNative(System.getProperty("user.dir") + "\\libs\\CVR100\\Termb.dll", "GetPeopleNation");
            n.setRetVal(Type.INT); // 指定返回参数的类型
            Pointer a = new Pointer(MemoryBlockFactory.createMemoryBlock(4 * 10));
            Pointer b = new Pointer(MemoryBlockFactory.createMemoryBlock(4 * 30));
            n.setParameter(0, b);
            n.setParameter(1, a);
            n.invoke();
            byte[] by = new byte[120];
            by = b.getMemory();
            try {
                strTmp = new String(by, "gb2312");
                people.put("PeopleNation", strTmp.trim());
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(CVR100DemoView.class.getName()).log(Level.SEVERE, null, ex);
            }

            byte[] bt = new byte[40];
            bt = a.getMemory();
            len = a.getAsInt(0);
            a.dispose();
            b.dispose();
            return Integer.parseInt(n.getRetVal());
        } finally {

        }
    }

    public int GetPeopleBirthday() throws NativeException, IllegalAccessException {
        JNative n = null;
        try {
            n = new JNative(System.getProperty("user.dir") + "\\libs\\CVR100\\Termb.dll", "GetPeopleBirthday");
            n.setRetVal(Type.INT); // 指定返回参数的类型
            System.out.println(n);
            Pointer a = new Pointer(MemoryBlockFactory.createMemoryBlock(4 * 10));
            Pointer b = new Pointer(MemoryBlockFactory.createMemoryBlock(4 * 30));
            n.setParameter(0, b);
            n.setParameter(1, a);
            n.invoke();
            byte[] by = new byte[120];
            by = b.getMemory();
            try {
                strTmp = new String(by, "gb2312");
                people.put("PeopleBirthday", strTmp.trim());
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(CVR100DemoView.class.getName()).log(Level.SEVERE, null, ex);
            }

            byte[] bt = new byte[40];
            bt = a.getMemory();
            len = a.getAsInt(0);
            a.dispose();
            b.dispose();
            return Integer.parseInt(n.getRetVal());
        } finally {

        }
    }

    public int GetPeopleAddress() throws NativeException, IllegalAccessException {
        JNative n = null;
        try {
            n = new JNative(System.getProperty("user.dir") + "\\libs\\CVR100\\Termb.dll", "GetPeopleAddress");
            n.setRetVal(Type.INT); // 指定返回参数的类型
            Pointer a = new Pointer(MemoryBlockFactory.createMemoryBlock(4 * 10));
            Pointer b = new Pointer(MemoryBlockFactory.createMemoryBlock(4 * 30));
            n.setParameter(0, b);
            n.setParameter(1, a);
            n.invoke();
            byte[] by = new byte[120];
            by = b.getMemory();
            try {
                strTmp = new String(by, "gb2312");
                people.put("PeopleAddress", strTmp.trim());
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(CVR100DemoView.class.getName()).log(Level.SEVERE, null, ex);
            }

            byte[] bt = new byte[40];
            bt = a.getMemory();
            len = a.getAsInt(0);
            a.dispose();
            b.dispose();
            return Integer.parseInt(n.getRetVal());
        } finally {

        }
    }


    public int GetPeopleIDCode() throws NativeException, IllegalAccessException {
        JNative n = null;
        try {
            n = new JNative(System.getProperty("user.dir") + "\\libs\\CVR100\\Termb.dll", "GetPeopleIDCode");
            n.setRetVal(Type.INT); // 指定返回参数的类型
            Pointer a = new Pointer(MemoryBlockFactory.createMemoryBlock(4 * 10));
            Pointer b = new Pointer(MemoryBlockFactory.createMemoryBlock(4 * 30));
            n.setParameter(0, b);
            n.setParameter(1, a);
            n.invoke();
            byte[] by = new byte[120];
            by = b.getMemory();
            try {
                strTmp = new String(by, "gb2312");
                people.put("PeopleIDCode", strTmp.trim());
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(CVR100DemoView.class.getName()).log(Level.SEVERE, null, ex);
            }

            len = a.getAsInt(0);

            a.dispose();
            b.dispose();
            return Integer.parseInt(n.getRetVal());
        } finally {

        }
    }


    public int GetDepartment() throws NativeException, IllegalAccessException {
        JNative n = null;
        try {
            n = new JNative(System.getProperty("user.dir") + "\\libs\\CVR100\\Termb.dll", "GetDepartment");
            n.setRetVal(Type.INT); // 指定返回参数的类型
            Pointer a = new Pointer(MemoryBlockFactory.createMemoryBlock(4 * 10));
            Pointer b = new Pointer(MemoryBlockFactory.createMemoryBlock(4 * 30));
            n.setParameter(0, b);
            n.setParameter(1, a);
            n.invoke();
            byte[] by = new byte[120];
            by = b.getMemory();
            try {
                strTmp = new String(by, "gb2312");
                people.put("GetDepartment", strTmp.trim());
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(CVR100DemoView.class.getName()).log(Level.SEVERE, null, ex);
            }

            len = a.getAsInt(0);

            a.dispose();
            b.dispose();
            return Integer.parseInt(n.getRetVal());
        } finally {

        }
    }


    public int GetStartDate() throws NativeException, IllegalAccessException {
        JNative n = null;
        try {
            n = new JNative(System.getProperty("user.dir") + "\\libs\\CVR100\\Termb.dll", "GetStartDate");
            n.setRetVal(Type.INT); // 指定返回参数的类型
            Pointer a = new Pointer(MemoryBlockFactory.createMemoryBlock(4 * 10));
            Pointer b = new Pointer(MemoryBlockFactory.createMemoryBlock(4 * 30));
            n.setParameter(0, b);
            n.setParameter(1, a);
            n.invoke();
            byte[] by = new byte[120];
            by = b.getMemory();
            try {
                strTmp = new String(by, "gb2312");
                people.put("StartDate", strTmp.trim());
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(CVR100DemoView.class.getName()).log(Level.SEVERE, null, ex);
            }

            len = a.getAsInt(0);

            a.dispose();
            b.dispose();
            return Integer.parseInt(n.getRetVal());
        } finally {

        }
    }

    public int GetEndDate() throws NativeException, IllegalAccessException {
        JNative n = null;
        try {
            n = new JNative(System.getProperty("user.dir") + "\\libs\\CVR100\\Termb.dll", "GetEndDate");
            n.setRetVal(Type.INT); // 指定返回参数的类型
            Pointer a = new Pointer(MemoryBlockFactory.createMemoryBlock(4 * 10));
            Pointer b = new Pointer(MemoryBlockFactory.createMemoryBlock(4 * 30));
            n.setParameter(0, b);
            n.setParameter(1, a);
            n.invoke();
            byte[] by = new byte[120];
            by = b.getMemory();
            try {
                strTmp = new String(by, "gb2312");
                people.put("EndDate", strTmp.trim());
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(CVR100DemoView.class.getName()).log(Level.SEVERE, null, ex);
            }

            len = a.getAsInt(0);

            a.dispose();
            b.dispose();
            return Integer.parseInt(n.getRetVal());
        } finally {

        }
    }

    @Override
    public void run() {
        setRead_ContentCode(1);
        flag = false;
        int statusCode = -1;
        try {
            CVR_InitComm(1001);
            flag = true;
            while (flag) {
                people.clear();
                statusCode = CVR_Authenticate();
                if (statusCode != 1) {
                    people.put("AuthenticateCode", -1);
                    continue;
                } else {
                    people.put("AuthenticateCode", 0);
                    people.put("AuthenticateMessage", "寻卡成功");
                }
                System.out.println(statusCode);
                statusCode = CVR_Read_Content(2);
                if (statusCode != 1) {
                    people.put("ContentCode", -1);
                    people.put("ContentMessage", "读卡失败");
                } else {
                    people.put("ContentCode", 0);
                    people.put("ContentMessage", "读卡成功");
                }
                GetPeopleName();

                GetPeopleSex();

                GetPeopleNation();

                GetPeopleBirthday();

                GetPeopleIDCode();

                GetPeopleAddress();

                GetStartDate();

                GetEndDate();

                TagListListiner();
                Thread.sleep(300);
            }
            setRead_ContentCode(-1);

        } catch (NativeException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        //关闭
    }

    public void TagListListiner() throws IOException, URISyntaxException {
        HttpClientUtil httpClientUtil = new HttpClientUtil();
        String filePath = System.getProperty("user.dir") + "\\conf.properties";
        //定义修改配置文件的工具类
        PropertiesUtils utils = new PropertiesUtils(filePath);
        people.put("machine_type", utils.getKeyValue("terminalSymbol"));
        people.put("terminalNumber", utils.getKeyValue("terminalNumber"));
        people.put("from", "idcard");
        System.out.println(people);
        websokectHandler sokect = new websokectHandler();
        sokect.onMessage(JSON.toJSONString(people));
//      HttpResponse response=httpClientUtil.HttoPost(people,"http://"+getIp()+":8080/DangD/getIDCardInfo");
        HttpResponse response = httpClientUtil.HttoPost(people, "http://" + utils.getKeyValue("serverIP") + ":" + utils.getKeyValue("serverPort") + "/DangD/getIDCardInfo");

    }

    public void closeIDCardDervice() {
        flag = false;
    }
}
